<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/21
 * Time: 15:56
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller\v1;

use app\common\business\BusShop;
use app\common\model\BuddhaArticle;
use app\common\model\BuddhaArticleIndex;
use app\common\model\BuddhaBanner;
use app\common\model\BuddhaBuddha;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\DiyHome;
use app\common\model\MallCate;
use app\common\model\MallGoods;
use app\common\model\MallOrder;
use app\common\model\NewsLists;
use app\common\model\NewsSee;
use think\App;

class Index extends Base
{
    /**
     * @return array|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIndexByListInfo()
    {
        //获取走进活佛文章
        $page = $this->request->param('page', 1);
        $limit = $this->request->param('limit', 10, 'intval');
        $cate = (new MallCate())->getMallCateByIndexList('id,title,image as logo');
        //获取店铺

        $shop = (new BusShop())->getBaseByList(['status' => 1], 'id,title');
        //获取购买
        $notice_list = (new MallOrder())->getBaseByListPage([['status','<>',0]],1,10,'id,realname');
        $notice = [];
        if(!empty($notice_list)){
            foreach ($notice_list as $v){
                $notice[] = [
                    "用户 ".mb_substr($v['realname'],0,1)."*".mb_substr($v['realname'],0,-1)." 下单成功"
                ];
            }
        }
        return Show::json_show(Status::mini_normal, "ok", [
            "cate" => $cate ?? [],
            "shop" => $shop ?? [],
            "title" => sysconfig('site', 'site_name'),
            "notice"=>$notice ?? []
        ]);
    }

    /**
     * 获取diy首页
     * @return array|\think\response\Json|void
     */
    public function getDiyHomeByList()
    {
        try {
            $diy = (new DiyHome())->getDiyHomeByList();
        } catch (\Exception $exception) {
            return Show::json_show(Status::mini_error, "error");
        }
        if ($diy) {
            $diy = $diy->toArray();
            $diy['content'] = Show::json_decode($diy['content']);
            return Show::json_show(Status::mini_normal, "ok", $diy);
        }
    }

    /**
     * 获取商品列表
     * @return array|\think\response\Json
     */
    public function goodsIndexListInfo()
    {
        $limit = $this->request->param('limit', 10, 'intval');
        $shop_id = $this->request->param('shop_id',0,'intval');
        $where = [];
        if ($shop_id != 0) {
            $where[] = [
                ["shop_id",'=',$shop_id]
            ];
        }
        try {
            $goods = (new MallGoods())->getPaginateByNormalListInfo($where, $limit, 'id,sku_id,title,thumb_title,price,market_price,logo', 'sort desc,id desc');
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "错误");
        }
        return Show::json_show(Status::mini_normal, "ok", $goods['data']);
    }

    /**
     * 获取首页文章
     * @return array|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNewsListInfo()
    {
        $limit = $this->request->param('limit', 10, 'intval');
        $page = $this->request->param('page', 1, 'intval');

        try {
            $row = (new NewsLists())->getPaginateByNormalListInfo([], $limit, "id,title,logo,images,create_time,type,sort,see_type,thumb_title", 'sort desc,id desc');
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "查询异常");
        }
        $lists = [];
        foreach ($row['data'] as $k => $v) {
            $lists[$k] = $v;
            $lists[$k]['time'] = strtotime($v['create_time']);
            $lists[$k]['images'] = explode('|', $v['images']);
            //查询查看次数
            $obj_news_see = new NewsSee();
            if ($v['see_type'] == 1) {
                $see = $obj_news_see->getUserByCount($v['id']);
            }else{
                $see = $obj_news_see->getSeeByCount($v['id']);
            }
            $lists[$k]['clickNum'] = $see['count'] ?? 0;

        }
        return Show::json_show(Status::mini_normal, "ok", $lists, $row['total'], false, $row['last_page']);
    }

}